package com.eca.emas.daemon.shared;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class EMASMailManager {
	private static EMASMailManager instance = new EMASMailManager();
	private ArrayList<Recipient> recipients;

	public static final int USER = 1;
	public static final int ADMIN = 2;

	public static final String ERROR_SQL = "Verbindung zum SQLServer fehlgeschlagen!";
	public static final String ERROR_WIREGATE = "Verbindung zum Wiregate Server fehlgeschlagen!";
	public static final String ERROR_NO_NEW_VALUES = "Fehler: Es wurden in den letzten 2 Stunden immer die gleichen Temperaturwerte aufgezeichnet!";

	private EMASMailManager() {
		recipients = new ArrayList<Recipient>();
		updateRecipients();
	}

	public void sendErrorMessageToAllRecipients(String message, Exception ex) {

		String detailedmessage = message + "\n"
				+ Arrays.toString(ex.getStackTrace());

		for (Recipient recipient : recipients) {
			if ((recipient.getRole() & ADMIN) > 0) {
				sendMessageToRecipient(detailedmessage,recipient);
			} else if ((recipient.getRole() & USER) > 0) {
				sendMessageToRecipient(message,recipient);
			}
		}
	}

	public void sendErrorMessageToAllRecipients(String message) {
		for (Recipient recipient : recipients) {
			sendMessageToRecipient(message, recipient);
		}
	}

	public void sendExceptionToAllRecipients(Exception ex) {
		for (Recipient recipient : recipients) {
			if ((recipient.getRole() & ADMIN) > 0) {
				sendErrorMessageToAllRecipients("" + ex.getStackTrace());
			}
		}
	}

	public void sendMessageToRecipient(String message, Recipient recipient) {
		sendMessage(message, recipient);
	}

	private void sendMessage(String text, Recipient recipient) {

		Authenticator authenticator = new Authenticator();

		Properties properties = new Properties();
		properties.setProperty("mail.smtp.submitter", authenticator
				.getPasswordAuthentication().getUserName());
		properties.setProperty("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.starttls.enable", "true");

		properties.setProperty("mail.smtp.host", "smtp.googlemail.com");
		properties.setProperty("mail.smtp.port", "25");

		Session session = Session.getInstance(properties, authenticator);

		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress("emasmngr@gmail.com"));
			message.addRecipient(RecipientType.TO, new InternetAddress(
					recipient.getMail_address()));

			message.setSubject("EMAS: Automatic Generated Message");
			message.setText(text);
			message.setSentDate(new Date());

			Transport.send(message);

		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}

	public static EMASMailManager getInstance() {
		return instance;
	}

	public void updateRecipients() {
		if (recipients == null) {
			recipients = new ArrayList<Recipient>();
		}

		recipients = MySQLConnectionManager.getInstance()
				.getAllMailRecipients();
	}

	private class Authenticator extends javax.mail.Authenticator {
		private PasswordAuthentication authentication;

		public Authenticator() {
			String username = "emasmngr@gmail.com";
			String password = "emas2012";
			authentication = new PasswordAuthentication(username, password);
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return authentication;
		}
	}
}
